<?php
	require_once("config.php");
	require_once("objects/conversation.php");
	
	$menu = new Menu("Other Actions");
    $menu->AddItem(new MenuItem("Browse Listings","browse.php", AuthConstants::ALL_LOGGED_IN));
    $menu->AddItem(new MenuItem("Search Listings","search.php", AuthConstants::ALL_LOGGED_IN));
    MenuManager::AddMenu("Conversations",$menu);
	
	if ($_GET['action'] == 'compose') {
		Functions::PrintHeader("Contact");

		$listing = new ListingRow($_GET['listingid']);
		$listingobj = ListingManager::FetchListing($_GET['listingid']);
		$listing->Render();
		?><br />
		<form method="POST" action="messages.php?action=send">
			<input type='hidden' name='listingid' value='<? echo $_GET['listingid']; ?>' />
			<input type='hidden' name='sender' value='<? echo AuthManager::GetUserID(); ?>' />
				<?php
					$conversation = new Conversation($_GET['listingid']);
					$rows = $conversation->GetAllMessages();
					$role = (AuthManager::ListingBelongsTo($_GET['listingid']) ? "landlord" : "student");
					
					if (count ($rows) > 0) {
						?>
						<table class='wide cgltable'>
							<tr>
								<th colspan='2'>Contact</th>
							</tr>
						<?php
						foreach ($rows as $row) {
							DBM::Execute("UPDATE conversations SET " . $role . "_attributes = " . Conversation::MESSAGE_READ . " WHERE id=$row[id];");
							// mark the message as read for this role
							echo "<tr>" .
									"<td style='width:20%; max-width:200px;'><i>" . date("M d, Y g:i A",$row['date']) . "</i> by <br />" .
									"<b>$row[composed_by]</b>" . 
									"</td>" .
									"<td>" . str_replace("\n","<br />",$row['message']) . "</td>" .
								"</tr>";
						}
						echo "</table>";
					}
					
					echo "<input type='hidden' name='role' value='$role' />";
					echo "<input type='hidden' name='recipient' value='" . $conversation->GetComposeRecipient("id") . "' />";
					echo "<input type='hidden' name='composed_by' value='" . AuthManager::GetUserID() . "' />";
				?>
			<br /><br />
			<table class='wide cgltable'>
				<tr><th colspan='2'>&nbsp;</th></tr>
				<tr>
					<td>To</td>
					<td><?php
						echo $conversation->GetComposeRecipient();
					?></td>
				</tr>
				<tr>
					<td>Message</td>
					<td>
						<textarea style='width:95%; height:150px;' name='message'></textarea>
					</td>
				</tr>
				<tr>
					<td colspan='2'><input type='submit' value="Send" /></td>
				</tr>
			</table>
		</form>
		<?php
		Functions::PrintFooter();
	}
	else if ($_GET['action'] == 'send') {
		// put in the conversation as unread messages
		$stuatt = 1;
		$lndatt = 1;

		// run the query
		$query = "INSERT INTO conversations (listingid, student, landlord, message, student_attributes, landlord_attributes, date, sender) " .
				"VALUES ($_POST[listingid],$_POST[sender],$_POST[recipient],'" . QueryTools::Escape($_POST['message']) . "',$stuatt,$lndatt," .
				time() . ",'$_POST[role]');";
				
		DBM::Execute($query);
		
		header("Location: messages.php");
	}
	else if ($_GET['action'] == 'delete') {
	
	}
	else {
        $userid = AuthManager::GetUserID();
		$query = "SELECT DISTINCT listingid FROM conversations WHERE student=$userid OR landlord=$userid;";
		$convos = DBM::FetchAllRows($query);
		$drows = array();
		
		foreach ($convos as $convo) {
			$drows[] = new Conversation($convo['listingid']);
		}
		
		Functions::PrintHeader("Conversations");
		echo "<table class='cgltable wide'>";
		echo "<tr>" . 
				"<th width='1%'>&nbsp;</th>" .
				"<th width='10%'>Messages</th>" .
				"<th>Conversation With</th>" . 
				"<th>Listing</th>" .
				"<th>Date</th>" .
			"</tr>";
				
		$ids = array();
		if (count($drows) == 0) {
			echo "<tr><td colspan='5'><i>You have no messages</i></td></tr>";
		}
		else {
			foreach ($drows as $conversation) {
				echo "<tr>" .
					"<td><input type='checkbox' name='convobox_$row[id]' /></td>" .
					"<td>" . $conversation->CountMessages() . "</td>" .
					"<td><a href='messages.php?action=compose&listingid=" . $conversation->listingobj->GetID() . "'>" . $conversation->GetComposeRecipient('name') . "</a></td>" . 
					"<td>" . $conversation->listingobj->GetAddress(true) . "</td>" .
					"<td>" . date("M d, Y g:i A",$conversation->GetMostRecentDate()) . "</td>" .
				"</tr>";
			}
		}
		echo "</table>";
		Functions::PrintFooter();
	}
?>